
package com.jf.cloud.search.feign;


import cn.hutool.core.collection.CollUtil;
import com.jf.cloud.api.feign.SearchOrderFeignClient;
import com.jf.cloud.api.vo.EsPageVO;
import com.jf.cloud.common.order.dto.OrderSearchDTO;
import com.jf.cloud.common.order.vo.EsOrderVO;
import com.jf.cloud.common.order.vo.OrderItemVO;
import com.jf.cloud.common.order.vo.SendNotifyBO;
import com.jf.cloud.common.order.vo.UserOrderStatisticVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.search.manager.OrderRefundSearchManager;
import com.jf.cloud.search.manager.OrderSearchManager;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Hidden;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单搜索feign连接
 * @author zz
 * @date 2020/12/07
 */
@RestController
@Hidden
public class SearchOrderFeignController implements SearchOrderFeignClient {

    @Autowired
    private OrderSearchManager orderSearchManager;
    @Autowired
    private OrderRefundSearchManager orderRefundSearchManager;
    @Autowired
    private MapperFacade mapperFacade;


//    @Override
//    public ServerResponseEntity<EsPageVO<EsOrderVO>> getOrderPage(OrderSearchDTO orderSearch) {
//        EsPageDTO pageDTO = new EsPageDTO();
//        pageDTO.setPageNum(orderSearch.getPageNum());
//        pageDTO.setPageSize(orderSearch.getPageSize());
//        return ServerResponseEntity.success(orderSearchManager.pageSearchResult(pageDTO, orderSearch));
//    }

//    @Override
//    public ServerResponseEntity<List<EsOrderVO>> getOrderList(OrderSearchDTO orderSearch) {
//        return ServerResponseEntity.success(orderSearchManager.listSearchResult(orderSearch));
//    }

    @Override
    public ServerResponseEntity<List<UserOrderStatisticVO>> countOrderByUserIds(List<Long> userIds) {
        List<UserOrderStatisticVO> list = orderSearchManager.countOrderByUserIds(userIds);
        return  ServerResponseEntity.success(list);
    }

    @Override
    public ServerResponseEntity<EsPageVO<EsOrderVO>> pageOrder(OrderSearchDTO orderSearchDTO) {
        return ServerResponseEntity.success(orderSearchManager.pageSearchResult(orderSearchDTO));
    }

    @Override
    public ServerResponseEntity<Long> countOrder(OrderSearchDTO orderSearchDTO) {
        orderSearchDTO.setPageSize(1);
        orderSearchDTO.setPageNum(1);
        EsPageVO<EsOrderVO> esOrderPage = orderSearchManager.pageSearchResult(orderSearchDTO);
        return ServerResponseEntity.success(esOrderPage.getTotal());
    }

    @Override
    public ServerResponseEntity<List<Map<String,Object>>> sumOrderGroupArea(OrderSearchDTO orderSearchDTO) {
        List<Map<String,Object>> orderList = orderSearchManager.sumOrderGroupArea(orderSearchDTO);
        return ServerResponseEntity.success(orderList);
    }

    @Override
    public ServerResponseEntity<List<EsOrderVO>> listOrderById(List<Long> orderIds) {
        if (CollUtil.isEmpty(orderIds)) {
            return ServerResponseEntity.success(new ArrayList<>());
        }
        List<EsOrderVO> orderList = orderSearchManager.listByOrderIds(orderIds);
        return ServerResponseEntity.success(orderList);
    }

    @Override
    public ServerResponseEntity<List<EsOrderVO>> listOrder(OrderSearchDTO orderSearchDTO) {
        EsPageVO<EsOrderVO> orderPage = orderSearchManager.pageSearchResult(orderSearchDTO);
        return ServerResponseEntity.success(orderPage.getList());
    }

    @Override
    public ServerResponseEntity<List<OrderItemVO>> getOrderItems(List<Long> orderItemIds) {
        if (CollUtil.isEmpty(orderItemIds)) {
            return ServerResponseEntity.success();
        }
        OrderSearchDTO orderSearchDTO = new OrderSearchDTO();
        orderSearchDTO.setOrderItemIds(orderItemIds);
        orderSearchDTO.setPageSize(orderItemIds.size());
        orderSearchDTO.setPageNum(1);
        orderSearchDTO.setFetchSource(new String[]{"orderId", "orderItems","actualTotal"});
        EsPageVO<EsOrderVO> orderPage = orderSearchManager.pageSearchResult(orderSearchDTO);
        List<OrderItemVO> orderItemList = new ArrayList<>();
        for (EsOrderVO esOrderVO : orderPage.getList()) {
            orderItemList.addAll(mapperFacade.mapAsList(esOrderVO.getOrderItems(), OrderItemVO.class));
        }
        return ServerResponseEntity.success(orderItemList);
    }

//    @Override
//    public ServerResponseEntity<List<OrderSimpleAmountInfoBO>> getOrdersSimpleAmountInfo(List<Long> orderIds) {
//        List<OrderSimpleAmountInfoBO> ordersSimpleAmountInfos = new ArrayList<>();
//        if (CollUtil.isEmpty(orderIds)) {
//            return ServerResponseEntity.success(ordersSimpleAmountInfos);
//        }
//        List<EsOrderVO> orderList = orderSearchManager.listByOrderIds(orderIds);
//        if (CollUtil.isEmpty(orderList)) {
//            return ServerResponseEntity.success(ordersSimpleAmountInfos);
//        }
//        OrderRefundSearchDTO orderRefundSearchDTO = new OrderRefundSearchDTO();
//        orderRefundSearchDTO.setOrderIds(orderIds);
//        orderRefundSearchDTO.setPageNum(1);
//        orderRefundSearchDTO.setPageSize(orderIds.size());
//        orderRefundSearchDTO.setReturnMoneyStsExclude(-1);
//        orderRefundSearchDTO.setRefundType(0);
//        EsPageVO<EsOrderRefundVO> orderRefundPage = orderRefundSearchManager.pageSearchResult(orderRefundSearchDTO);
//        Map<Long, List<EsOrderRefundVO>> refundMap = orderRefundPage.getList().stream().collect(Collectors.groupingBy(EsOrderRefundVO::getOrderId));
//
//        for (EsOrderVO order : orderList) {
//            OrderSimpleAmountInfoBO ordersSimpleAmountInfo = mapperFacade.map(order, OrderSimpleAmountInfoBO.class);
//            ordersSimpleAmountInfos.add(ordersSimpleAmountInfo);
//            Map<Long, EsOrderItemVO> orderItemMap = order.getOrderItems().stream().collect(Collectors.toMap(EsOrderItemVO::getOrderItemId, o -> o));
//            long purchaseAmount = 0L;
//            long actualTotal = 0L;
//            long purchasePlatformCommission = 0L;
//            if(refundMap.containsKey(ordersSimpleAmountInfo.getOrderId()) && !Objects.equals(ordersSimpleAmountInfo.getSupplierId(),0L)){
//                // 减去退款的实付金额，采购金额，采购佣金
//                List<EsOrderRefundVO> orderRefunds = refundMap.get(ordersSimpleAmountInfo.getOrderId());
//                for (EsOrderRefundVO orderRefund : orderRefunds) {
//                    EsOrderItemVO esOrderItemVO = orderItemMap.get(orderRefund.getOrderItemId());
//                    if (Objects.isNull(esOrderItemVO)) {
//                        continue;
//                    }
//                    purchaseAmount += esOrderItemVO.getItemPurchaseAmount();
//                    actualTotal += esOrderItemVO.getActualTotal();
//                    purchasePlatformCommission += orderRefund.getPurPlatformRefundCommission();
//                }
//                ordersSimpleAmountInfo.setPurchaseAmount(ordersSimpleAmountInfo.getPurchaseAmount() - purchaseAmount);
//                ordersSimpleAmountInfo.setActualTotal(ordersSimpleAmountInfo.getActualTotal() - actualTotal);
//                ordersSimpleAmountInfo.setPurchasePlatformCommission(ordersSimpleAmountInfo.getPurchasePlatformCommission() - purchasePlatformCommission);
//            }
//
//            ordersSimpleAmountInfo.setShopAmount(ordersSimpleAmountInfo.getReduceAmount() - ordersSimpleAmountInfo.getPlatformAmount());
//            ordersSimpleAmountInfo.setDistributionAmount(0L);
//            // 订单总分销金额
//            for (EsOrderItemVO orderItem : order.getOrderItems()) {
//                ordersSimpleAmountInfo.setDistributionAmount(ordersSimpleAmountInfo.getDistributionAmount() + orderItem.getDistributionAmount() + orderItem.getDistributionParentAmount());
//                ordersSimpleAmountInfo.setPurchasePlatformCommission(orderItem.getPurchasePlatformCommission());
//            }
//        }
//
//        return ServerResponseEntity.success(ordersSimpleAmountInfos);
//    }


    @Override
    public ServerResponseEntity<List<SendNotifyBO>> listSendNotifyByOrderIds(List<Long> orderIds) {
        List<EsOrderVO> esOrderList = orderSearchManager.listByOrderIds(orderIds);
        List<SendNotifyBO> sendNotifyList = new ArrayList<>();
        for (EsOrderVO esOrderVO : esOrderList) {
            SendNotifyBO sendNotifyBO = mapperFacade.map(esOrderVO, SendNotifyBO.class);
            sendNotifyBO.setBizId(esOrderVO.getOrderId());
            sendNotifyBO.setSpuName(esOrderVO.getOrderItems().get(0).getSpuName());
            sendNotifyList.add(sendNotifyBO);
        }
        return ServerResponseEntity.success(sendNotifyList);
    }

    @Override
    public ServerResponseEntity<List<EsOrderVO>> listEsOrder(OrderSearchDTO orderSearchDTO) {
        List<EsOrderVO> esOrders = orderSearchManager.listSearchResult(orderSearchDTO);
        return ServerResponseEntity.success(esOrders);
    }
}
